<!-- 审批用户发布通知公告页面 -->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="抽取项目" prop="extractItem">
        <el-input
          v-model="queryParams.extractItem"
          placeholder="请输入抽取项目"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="抽取人单位" prop="createBy">
      	<el-select v-model="form.createBy" placeholder="请选择抽取人单位" clearable size="small" style="width:100%;" @change="UnitTypeChangepenson">
      		<el-option v-for="type in unitTypeponsonOptions" :key="type.userId" :label="type.nickName" :value="type.userId" />
      	</el-select>
      </el-form-item>
      <el-form-item label="抽取库" prop="randomUnittypeId">
        <el-select v-model="form.randomUnittypeId" placeholder="请选择单位库" clearable size="small" style="width:100%;" @change="UnitTypeChange">
          <el-option
            v-for="type in unitTypeOptions"
            :key="type.unittypeId"
            :label="type.unittypeName"
            :value="type.unittypeId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="extractList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="抽取项目" align="center" prop="extractItem" />
      <el-table-column label="抽取人" align="center" prop="extractUser" />
      <el-table-column label="抽取人单位" align="center" prop="extractUnit" />
      <el-table-column label="抽取库" align="center" prop="randomUnittypeId" :formatter="unitTypeFormat"/>
      <el-table-column label="审批状态" align="center" prop="extractApproveStatus" :formatter="approveStatusFormat"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
          >结果查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-message"
          >公告发布</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改项目申请对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="抽取库" prop="randomUnittypeId">
              <el-select v-model="form.randomUnittypeId" disabled='disabled' placeholder="请选择单位库" clearable size="small" style="width:100%;" @change="UnitTypeChange">
                <el-option
                  v-for="type in unitTypeOptions"
                  :key="type.unittypeId"
                  :label="type.unittypeName"
                  :value="type.unittypeId"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="抽取项目" prop="extractItem">
              <el-input v-model="form.extractItem" placeholder="请输入抽取项目" disabled='disabled'/>
              <el-input v-model="form.extractId" disabled='disabled' style="display:none;"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="抽取人" prop="extractUser">
              <el-input v-model="form.extractUser" placeholder="请输入抽取人" disabled='disabled'/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="抽取人单位" prop="extractUnit">
              <el-input v-model="form.extractUnit" placeholder="请输入抽取人单位" disabled='disabled'/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="排除要求" prop="extractBanReason">
          <el-input v-model="form.extractBanReason" type="textarea" placeholder="请输入内容" disabled='disabled'/>
        </el-form-item>
        <el-form-item label="备    注" prop="extractRemarks">
          <el-input v-model="form.extractRemarks" type="textarea" placeholder="请输入内容" disabled='disabled'/>
        </el-form-item>

        <!-- 库下对应单位表格 -->
        <el-table v-loading="loading" id="unitTable" :data="unitList">
          <el-table-column label="单位序号" align="center" prop="unitSort" />
          <el-table-column label="单位名称" align="center" prop="unitName" />
          <el-table-column label="资质等级" align="center" prop="unitNaturalGrade" />
          <el-table-column label="资质范围" align="center" prop="unitNaturalScope" />
          <el-table-column label="注册地址" align="center" prop="unitAddress" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                v-hasPermi="['unit:random:edit']"
              >禁用</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="extractAdd">抽 取</el-button>
        <el-button @click="cancel">重 置</el-button>
      </div>
    </el-dialog>

    <!-- 抽签过程展示窗口 -->
    <el-dialog :title="title" :visible.sync="extractOpen" width="500px" append-to-body>
      <img src="../../../assets/images/random.gif" class="user-avatar" style="height:200px;margin-left:25%;">
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">停 止</el-button>
        <el-button @click="gifcancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看项目抽签结果对话框 -->
    <el-dialog :title="title" :visible.sync="resultOpen" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="抽取库" prop="randomUnittypeId">
              <el-select v-model="form.randomUnittypeId" disabled='disabled' placeholder="请选择单位库" clearable size="small" style="width:100%;" @change="UnitTypeChange">
                <el-option
                  v-for="type in unitTypeOptions"
                  :key="type.unittypeId"
                  :label="type.unittypeName"
                  :value="type.unittypeId"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="抽取项目" prop="extractItem">
              <el-input v-model="form.extractItem" placeholder="请输入抽取项目" disabled='disabled'/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="抽取人" prop="extractUser">
              <el-input v-model="form.extractUser" placeholder="请输入抽取人" disabled='disabled'/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="抽取人单位" prop="extractUnit">
              <el-input v-model="form.extractUnit" placeholder="请输入抽取人单位" disabled='disabled'/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="排除要求" prop="extractBanReason">
          <el-input v-model="form.extractBanReason" type="textarea" placeholder="请输入内容" disabled='disabled'/>
        </el-form-item>
        <el-form-item label="备    注" prop="extractRemarks">
          <el-input v-model="form.extractRemarks" type="textarea" placeholder="请输入内容" disabled='disabled'/>
        </el-form-item>

        <!-- 当前项目抽签结果情况表格 -->
        <el-table v-loading="loading" :data="randomResultList" @selection-change="handleSelectionChange">
          <el-table-column label="单位名称" align="center" prop="randomUnitName" />
          <el-table-column label="抽取时间" align="center" prop="createTime" />
          <el-table-column label="抽取状态" align="center" prop="randomBan" :formatter="randomBanFormat"/>
        </el-table>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { listExtract, getExtract, delExtract, addExtract, updateExtract, exportExtract } from "@/api/unit/extract";
import { listRandom, getRandom, delRandom, addRandom, updateRandom, exportRandom } from "@/api/unit/random";
import { listUnit,randomlist, getUnit, delUnit, addUnit, updateUnit, exportUnit } from "@/api/unit/unit";
import { listUnittype, getUnittype } from "@/api/unit/unittype";
import { listUser } from '@/api/system/user';

export default {
  name: "Extract",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目申请表格数据
      extractList: [],
      // 单位信息表格数据
      unitList: [],
      // 单位抽签表格数据
      randomList: [],
      //抽签结果表格数据
      randomResultList:[],
      //抽取人单位字典
	  unitTypeponsonOptions: [],
      // 单位类型字典
      unitTypeOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //抽取弹出层
      extractOpen:false,
      //项目抽签结果层
      resultOpen:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
		extractId:null,
        extractItem: null,
        extractUser: null,
        extractUnit: null,
        extractApproveStatus: null,
        randomUnittypeId: null,
      },
      UnitqueryParams: {
        pageNum: 1,
        pageSize: 10,
        unitName: null,
        unitNaturalGrade: null,
        unitNaturalScope: null,
        unitType: null,
      },
      // 查询参数
      RandomqueryParams: {
        pageNum: 1,
        pageSize: 10,
        extractId: null,
        randomUnitId: null,
        annex: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        extractUser: [
          { required: true, message: "抽取人不能为空", trigger: "blur" }
        ],
        extractUnit: [
          { required: true, message: "抽取单位不能为空", trigger: "blur" }
        ],
        extractItem: [
          { required: true, message: "抽取项目不能为空", trigger: "blur" }
        ],
        randomUnittypeId: [
          { required: true, message: "抽取库不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
    listUnittype(this.unitTypeParams).then(response => {
        this.unitTypeOptions = response.rows;
      });
	  listUser().then(response => {
	  	this.unitTypeponsonOptions = response.rows;
	  });
  },
  methods: {
    /** 查询项目申请列表 */
    getList() {
      this.loading = true;
	  this.queryParams.extractApproveStatus="8";
	  this.queryParams.randomUnittypeId = this.form.randomUnittypeId;
      listExtract(this.queryParams).then(response => {
        this.extractList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        extractId: null,
        extractItem: null,
        extractUser: null,
        extractUnit: null,
        extractRemarks: null,
        extractBanReason: null,
        extractScore: null,
        extractApproveStatus: "1",
        randomUnittypeId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    // 单位库类型翻译
    unitTypeFormat(row, column) {
      return this.selecttypeLabel(this.unitTypeOptions, row.randomUnittypeId);
    },
    //转换单位库类型
    selecttypeLabel(datas, value) {
      var actions = [];
      Object.keys(datas).some((key) => {
        if (datas[key].unittypeId == ('' + value)) {
          actions.push(datas[key].unittypeName);
          return true;
        }
      })
      return actions.join('');
    },
	//抽取人单位
	UnitTypeChangepenson(value) {
		listUser().then(response => {
			for(var o=0;o<response.rows.length;o++){
				if(value == response.rows[o].userId){
					this.queryParams.createBy = response.rows[o].nickName;
				}
			}
		});
	},
    approveStatusFormat(row, column){
     return this.getType(row,column);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    //单位选择select改变方法
    UnitTypeChange(value){
      //让table显现出来

      //查询table数据
      this.UnitqueryParams.unitType = value;
      randomlist(this.UnitqueryParams).then(response => {
        this.unitList = response.rows;
        this.loading = false;
      });
    },
    /** 重置按钮操作 */
    resetQuery() {
		this.reset();
      this.resetForm("queryForm");
      this.handleQuery();
    },
    //抽取动画取消
    gifcancel() {
      this.extractOpen = false;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.extractId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 抽取按钮操作 */
    extractAdd() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.extractOpen = true;
        }
      });
    },
    /** 抽签按钮操作 */
    handleUpdate(row) {
      this.reset();
      const extractId = row.extractId || this.ids
      getExtract(extractId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "项目单位抽签";
      });

      //查询单位table数据
      this.UnitqueryParams.unitType = row.randomUnittypeId;
      randomlist(this.UnitqueryParams).then(response => {
        this.unitList = response.rows;
        this.loading = false;
      });
    },
    /** 查看抽签结果按钮 */
    resultShow(row) {
      const extractId = row.extractId || this.ids
      getExtract(extractId).then(response => {
        this.form = response.data;
        this.resultOpen = true;
        this.title = "项目抽签结果";
      });

      //查询单位table数据
      this.RandomqueryParams.extractId = row.extractId || this.ids;
      listRandom(this.RandomqueryParams).then(response => {
        this.randomResultList = response.rows;
        this.loading = false;
      });
    },
	//公告发布
	Announcement(row) {
	 if (row.extractId != null) {
		this.queryParams.extractId = row.extractId;
		this.queryParams.extractApproveStatus = 5;
		updateExtract(this.queryParams).then(response => {
			 this.msgSuccess("发布成功");
			 this.open = false;
			 this.getList();
		});
	 }
	},
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.extractOpen = false;
          if (this.form.extractId != null) {
            addRandom(this.form).then(response => {
              this.reset();
              this.open = false;
              this.msgSuccess("抽取成功");
            });
          } else {
            
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const extractIds = row.extractId || this.ids;
      this.$confirm('是否确认删除项目申请编号为"' + extractIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delExtract(extractIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 单位抽取状态转换 */
    randomBanFormat(row, column) {
      if(row.randomBan == 0){
        return "中签"
      }else{
        return "排除"
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有项目申请数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportExtract(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
